<script setup>
import { ref } from "vue";
import { uploadUtils } from "@/composables/uploadUtils.js";
import { useUserStore } from "@/stores/user";

const userStore = useUserStore();

const {
  imageUrl,
  handleAvatarSuccess,
  beforeAvatarUpload
} = uploadUtils();

const form = ref({
  name: "张三",
  sex: "男",
  phone: "18888888888",
});

const isForm = ref(true);

const submit = () => {
  ElMessageBox.confirm(
      "确认提交?",
      {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }
  )
      .then(() => {
        //删除操作
        isForm.value = true;
      })
      .catch(() => {
        //取消
        isForm.value = true;
      });
};
</script>

<template>
  <el-card>
    <el-form v-if="isForm" :model="form" label-width="120px">
      <el-form-item label="姓名">
        <el-input disabled v-model="form.name"/>
      </el-form-item>
      <el-form-item label="手机号码">
        <el-input disabled v-model="form.phone"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="isForm = false">修改</el-button>
      </el-form-item>
    </el-form>

    <el-form v-else :model="form" label-width="120px">
      <el-form-item label="姓名">
        <el-input v-model="form.name"/>
      </el-form-item>
      <el-form-item label="手机号码">
        <el-input v-model="form.phone"/>
      </el-form-item>
      <el-form-item label="头像">
        <el-upload
            class="avatar-uploader"
            action="https://run.mocky.io/v3/9d059bf9-4660-45f2-925d-ce80ad6c4d15"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
        >
          <img v-if="imageUrl" :src="imageUrl" class="avatar"/>
          <el-icon v-else class="avatar-uploader-icon">
            <Plus/>
          </el-icon>
        </el-upload>
      </el-form-item>
      <el-form-item>
        <el-button style="align-content: center" type="primary" @click="submit">提交</el-button>
        <el-button style="align-content: center" type="primary" @click="isForm = true">取消</el-button>
      </el-form-item>
    </el-form>
  </el-card>
</template>

<style scoped lang="scss">
.avatar-uploader .avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed var(--el-border-color);
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  text-align: center;
}
</style>
